<div class="col-mg-12" style="text-align: center;">
    <h3>目录管理</h3>
</div>

<div class="col-md-12 tabs-div">
    <ul class="nav nav-tabs">
        <li role="presentation" class="active"><a href="/sys/menu">新增目录</a></li>
    </ul>
</div>

<div class="col-xs-12">
    <div class="space-12"></div>
    <form class="form-horizontal" id="user_form" action="" method="post" enctype="" onsubmit="return false;">
        <div class="form-group col-sm-7">
            <label for="password" class="col-sm-5 control-label">目录ID：</label>
            <div class="col-sm-7">
                <input type="text" id="level_sign" name="level_sign" disabled="disabled" class="col-sm-7" />
            </div>
        </div>
        <div class="form-group col-sm-7">
            <label for="password" class="col-sm-5 control-label">目录层级：</label>
            <div class="col-sm-7">
                <select name="menu_level">
                    <option value="0">-- 请选择 --</option>
                    <option value="2">二级目录</option>
                    <option value="3">三级目录</option>
                </select>
            </div>
        </div>

        <div class="form-group col-sm-7 display" id="level2-list">
            <label for="password" class="col-sm-5 control-label">所属分组：</label>
            <div class="col-sm-7">
                <select id="parent_id" name="parent_id">
                    <option value="0">-- 请选择 --</option>
                    <?php
                    foreach ($level2List as $level2) {
                        ?>
                        <option value="<?=$level2['id']?>"><?=$level2['title']?></option>
                        <?php
                    }
                    ?>
                </select>
            </div>
        </div>

        <div class="form-group col-sm-7">
            <label for="password" class="col-sm-5 control-label">页面标题：</label>
            <div class="col-sm-7">
                <input type="text" id="title" name="title" class="col-sm-7"  />
            </div>
        </div>

        <div class="form-group col-sm-7">
            <label for="password" class="col-sm-5 control-label">URI：</label>
            <div class="col-sm-7">
                <input type="text" id="uri" name="uri" class="col-sm-7" />
            </div>
        </div>
        <div class="form-group col-sm-7 center">
            <button type="button" class="btn btn-primary btn-sm" onclick="saveInfo();">保存</button>
        </div>
    </form>
</div>

<script>
    var nextLevel2 = '<?=$nextLevel2?>';
    var ajaxUrl = '/sys/menu/ajax';
    var level2List = {
    <?php
    foreach ($level2List as $level2) {
    ?>
    <?=$level2['id']?> : '<?=$level2['next_son']?>',
    <?php
    }
    ?>
    };
    $(function(){
        $("select[name=menu_level]").change(function(){
            var level = $(this).val();

            if (level == 2) {
                $("#level_sign").val(nextLevel2);
                $("#level2-list").hide();
            } else {
                $("#level_sign").val("");
                $("#level2-list").show();
            }
        });

        $("select[name=parent_id]").change(function(){
            var pid = $(this).val();
            if (pid != '0') {
                $("#level_sign").val(level2List[pid]);
            } else {
                $("#level_sign").val('');
            }
        });
    });

    function saveInfo() {
        var menuLevel = $("select[name=menu_level]").val();
        var levelSign = $("#level_sign").val();

        if (menuLevel == 3) {
            var menuGroup = $("#parent_id").val();
        } else {
            var menuGroup = 0;
        }

        var menuTitle = $("#title").val();
        var menuUri = $("#uri").val();

        var postData = {
            action      : 'addMenu',
            menu_title  : menuTitle,
            parent_id   : menuGroup,
            level_sign  : levelSign,
            menu_uri    : menuUri,
            menu_level  : menuLevel
        };

        console.log(postData);

        //alert("save");
        $.ajax({
            type : "POST",
            url  : ajaxUrl,
            data : postData,
            dataType : "json",
            success : function(res) {
                console.log(res);
                if (res.status != 200) {
                    alert(res.msg);
                    return false;
                } else {
                    alert("新增菜单成功!")
                    window.location.reload();
                }
            }
        });
    }
</script>